<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>音频上传测试</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        
        .header {
            background: linear-gradient(135deg, #ff6b6b, #ee5a52);
            color: white;
            padding: 30px;
            text-align: center;
        }
        
        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
        }
        
        .header p {
            opacity: 0.9;
            font-size: 1.1em;
        }
        
        .form-container {
            padding: 40px;
        }
        
        .form-group {
            margin-bottom: 25px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #333;
            font-size: 1.1em;
        }
        
        .form-group input[type="text"] {
            width: 100%;
            padding: 15px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 16px;
            transition: all 0.3s ease;
        }
        
        .form-group input[type="text"]:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }
        
        .file-input-container {
            position: relative;
            display: inline-block;
            width: 100%;
        }
        
        .file-input {
            width: 100%;
            padding: 15px;
            border: 2px dashed #ccc;
            border-radius: 8px;
            background: #f8f9fa;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .file-input:hover {
            border-color: #667eea;
            background: #f0f2ff;
        }
        
        .file-input input[type="file"] {
            position: absolute;
            opacity: 0;
            width: 100%;
            height: 100%;
            cursor: pointer;
        }
        
        .file-info {
            margin-top: 10px;
            padding: 10px;
            background: #e8f5e8;
            border-radius: 5px;
            color: #2d5a2d;
            display: none;
        }
        
        .submit-btn {
            width: 100%;
            padding: 18px;
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 18px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-top: 20px;
        }
        
        .submit-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 25px rgba(102, 126, 234, 0.3);
        }
        
        .submit-btn:disabled {
            background: #ccc;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }
        
        .result {
            margin-top: 30px;
            padding: 20px;
            border-radius: 8px;
            display: none;
        }
        
        .result.success {
            background: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        
        .result.error {
            background: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        
        .loading {
            display: none;
            text-align: center;
            margin: 20px 0;
        }
        
        .spinner {
            border: 3px solid #f3f3f3;
            border-top: 3px solid #667eea;
            border-radius: 50%;
            width: 40px;
            height: 40px;
            animation: spin 1s linear infinite;
            margin: 0 auto;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .audio-player {
            margin-top: 15px;
            width: 100%;
        }
        
        .required {
            color: #e74c3c;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🎵 音频上传系统</h1>
            <p>上传音频文件并同时添加歌名和原唱信息</p>
        </div>
        
        <div class="form-container">
            <form id="uploadForm">
                <div class="form-group">
                    <label for="songName">歌名 <span class="required">*</span></label>
                    <input type="text" id="songName" name="songName" placeholder="请输入歌曲名称" required>
                </div>
                
                <div class="form-group">
                    <label for="artistName">原唱 <span class="required">*</span></label>
                    <input type="text" id="artistName" name="artistName" placeholder="请输入原唱歌手名称" required>
                </div>
                
                <div class="form-group">
                    <label for="audioFile">音频文件 <span class="required">*</span></label>
                    <div class="file-input-container">
                        <div class="file-input" onclick="document.getElementById('audioFile').click()">
                            <input type="file" id="audioFile" name="audio" accept="audio/*" required>
                            <div id="fileText">
                                点击选择音频文件<br>
                                <small>支持 MP3, WAV, M4A, AAC, FLAC 格式，最大50MB</small>
                            </div>
                        </div>
                        <div class="file-info" id="fileInfo"></div>
                    </div>
                </div>
                
                <button type="submit" class="submit-btn" id="submitBtn">
                    🚀 上传音频
                </button>
            </form>
            
            <div class="loading" id="loading">
                <div class="spinner"></div>
                <p>正在上传，请稍候...</p>
            </div>
            
            <div class="result" id="result"></div>
        </div>
    </div>

    <script>
        const form = document.getElementById('uploadForm');
        const fileInput = document.getElementById('audioFile');
        const fileInfo = document.getElementById('fileInfo');
        const fileText = document.getElementById('fileText');
        const submitBtn = document.getElementById('submitBtn');
        const loading = document.getElementById('loading');
        const result = document.getElementById('result');

        // 文件选择处理
        fileInput.addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                const fileSize = (file.size / 1024 / 1024).toFixed(2);
                fileInfo.innerHTML = `
                    <strong>已选择文件:</strong> ${file.name}<br>
                    <strong>文件大小:</strong> ${fileSize} MB<br>
                    <strong>文件类型:</strong> ${file.type}
                `;
                fileInfo.style.display = 'block';
                fileText.innerHTML = `已选择: ${file.name}`;
            }
        });

        // 表单提交处理
        form.addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const songName = document.getElementById('songName').value.trim();
            const artistName = document.getElementById('artistName').value.trim();
            const audioFile = fileInput.files[0];
            
            // 验证输入
            if (!songName || !artistName || !audioFile) {
                showResult('请填写所有必填字段并选择音频文件', 'error');
                return;
            }
            
            // 显示加载状态
            submitBtn.disabled = true;
            submitBtn.textContent = '正在上传...';
            loading.style.display = 'block';
            result.style.display = 'none';
            
            try {
                // 创建FormData
                const formData = new FormData();
                formData.append('audio', audioFile);
                formData.append('songName', songName);
                formData.append('artistName', artistName);
                
                // 发送请求
                const response = await fetch('/api/upload/complete', {
                    method: 'POST',
                    body: formData
                });
                
                const responseData = await response.json();
                
                if (response.ok && responseData.code === 0) {
                    // 上传成功
                    const data = responseData.data;
                    let resultHTML = `
                        <h3>✅ 上传成功！</h3>
                        <p><strong>歌曲ID:</strong> ${data.songId}</p>
                        <p><strong>歌名:</strong> ${data.songName}</p>
                        <p><strong>原唱:</strong> ${data.artistName} (ID: ${data.artistId})</p>
                        <p><strong>文件大小:</strong> ${(data.size / 1024 / 1024).toFixed(2)} MB</p>
                        <p><strong>播放地址:</strong> <a href="${data.audioUrl}" target="_blank">${data.audioUrl}</a></p>
                    `;
                    
                    // 添加音频播放器
                    if (data.audioUrl) {
                        resultHTML += `
                            <audio controls class="audio-player">
                                <source src="${data.audioUrl}" type="audio/mpeg">
                                您的浏览器不支持音频播放。
                            </audio>
                        `;
                    }
                    
                    showResult(resultHTML, 'success');
                    
                    // 重置表单
                    form.reset();
                    fileInfo.style.display = 'none';
                    fileText.innerHTML = `
                        点击选择音频文件<br>
                        <small>支持 MP3, WAV, M4A, AAC, FLAC 格式，最大50MB</small>
                    `;
                    
                } else {
                    // 上传失败
                    showResult(`❌ 上传失败: ${responseData.message || '未知错误'}`, 'error');
                }
                
            } catch (error) {
                console.error('上传错误:', error);
                showResult(`❌ 上传失败: ${error.message}`, 'error');
            } finally {
                // 恢复按钮状态
                submitBtn.disabled = false;
                submitBtn.textContent = '🚀 上传音频';
                loading.style.display = 'none';
            }
        });

        function showResult(message, type) {
            result.innerHTML = message;
            result.className = `result ${type}`;
            result.style.display = 'block';
            result.scrollIntoView({ behavior: 'smooth' });
        }

        // 页面加载时检查服务器状态
        window.addEventListener('load', async function() {
            try {
                const response = await fetch('/health');
                const data = await response.json();
                console.log('服务器状态:', data);
            } catch (error) {
                console.warn('无法连接到服务器:', error);
                showResult('⚠️ 无法连接到服务器，请确保服务器正在运行', 'error');
            }
        });
    </script>
</body>
</html>